package com.onetech.adrewards.free.activities;

import android.graphics.Color;
import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.analytics.tracking.android.EasyTracker;
import com.onetech.adrewards.free.R;
import com.onetech.adrewards.free.config.AppConfig;
import com.onetech.adrewards.free.dialogs.SuccessDialog;
import com.onetech.adrewards.free.models.CommonModel;
import com.onetech.adrewards.free.objects.ResultDTO;
import com.onetech.adrewards.free.utils.AdrewadsUtils;
import com.todddavies.components.progressbar.ProgressWheel;

public class InviteGetPointActivity extends BaseActivity {
	private TextView mMessage1=null;
	private TextView mMessage2=null;
	
	public  String inviteCode = "";
	public String memId = "";
	
	private View mGetCoinBtn=null;
	private TextView mGetCoinBtnText = null;
	private ProgressWheel mProgressBar=null;
	
	private long mCurrentTime = 0;
	private boolean isSending=false;
	
	private GetCoinDataTask mGetCoinDataTask=null;
	ResultDTO mResult=null;
	
	private int buttonPressId=-1; 
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setMainView(R.layout.activity_invite_get_point_layout);
		setAppTittle(getString(R.string.app_name));
		showMyPoints(false);
		initView();
		initListener();
		
		
	}
	
	private void initView() {
		mMessage1=(TextView)findViewById(R.id.msg1);
		mMessage2=(TextView)findViewById(R.id.msg2);
		mGetCoinBtnText = (TextView)findViewById(R.id.getCoinBtnText);
		
		mGetCoinBtn = (LinearLayout)findViewById(R.id.getCoinBtn);
		
		
		mProgressBar=(ProgressWheel)findViewById(R.id.splashProgressBar);
		mProgressBar.setText("");
		mProgressBar.setContourColor(Color.TRANSPARENT);
		mProgressBar.spin();
		mProgressBar.setVisibility(View.INVISIBLE);
		
		Bundle bundle = getIntent().getExtras();
		 
		 if(bundle != null)
		 {
			 inviteCode = bundle.getString("inviteCode");
			 memId = bundle.getString("memId");
			 String coin = bundle.getString("coin");
			 if(coin != null)
			 {
				 mGetCoinBtnText.setText(coin +  "pt" + mGetCoinBtnText.getText());
				 mMessage1.setText(mMessage1.getText() + " " + inviteCode);
			 }
			 
			 
			 
		 }
		 
		 
				
		showFooterView(false);
	}
	
	private void initListener() {
		mGetCoinBtn.setOnClickListener(this);
		
	}
	
	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {	
		case R.id.getCoinBtn:
			
			if(inviteCode.length()<1 || memId == null || !memId.equals(AppConfig.uId)){
				AdrewadsUtils.showMessage(getApplicationContext(), getString(R.string.invite_get_point_warning));
				return;
			}
			buttonPressId=R.id.getCoinBtn;
			if(isSending){
				return;
			}
			long mTime=System.currentTimeMillis();
			long time=(mTime-mCurrentTime)/1000;
			if(time > 3){
				mCurrentTime=mTime;
				GetCoinDataTask();				
			}else{
				mCurrentTime=mTime;
				Toast.makeText(getApplicationContext(), getString(R.string.login_invite_code_warning), Toast.LENGTH_LONG).show();
			}
			break;
		
		default:
			super.onClick(v);
			return;
		}		
	}
	@Override
	protected void onResume() {
		super.onResume();
		if(mResult==null){
			getUserInfo();
		}
	}
	@Override
	public void finish() {
		super.finish();
	}
	private void GetCoinDataTask(){
		if(mGetCoinDataTask==null || mGetCoinDataTask.getStatus()==Status.FINISHED){
			mGetCoinDataTask=new GetCoinDataTask();
			mGetCoinDataTask.execute();
		}
	}
	
	private class GetCoinDataTask extends AsyncTask<Void, Void, ResultDTO>{
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			isSending=true;
			mProgressBar.setVisibility(View.VISIBLE);
		}
		@Override
		protected ResultDTO doInBackground(Void... params) {
			try {				
				return CommonModel.getInstance().doInviteCoin(inviteCode);
				
			} catch (Exception e) {
				return null;
			}			
		}
		@Override
		protected void onPostExecute(ResultDTO result) {
			super.onPostExecute(result);
			try {
				if(result!=null){
					mResult=result;
					if(buttonPressId==R.id.getCoinBtn){
						if(!"0".equals(result.getError_code())){
							AdrewadsUtils.showMessage(getApplicationContext(), result.getError_message());
							return;
						}else{
							if(result.getSuccessMessage()!=null && result.getSuccessMessage().trim().length()>0){
								SuccessDialog mDialog=new SuccessDialog(InviteGetPointActivity.this, getString(R.string.success_dialog_title), result.getSuccessMessage(), "home");
								
								mDialog.show();
							}else { 
								go2HomePage();
							}
						}
					}else{
						go2HomePage();
					}
				}else{
					AdrewadsUtils.showMessage(getApplicationContext(), getString(R.string.server_error_message));
					
					
				}
			} catch (Exception e) {
				AdrewadsUtils.showMessage(getApplicationContext(), getString(R.string.server_error_message));
			}
			finally{
				mProgressBar.setVisibility(View.INVISIBLE);
				isSending=false;
			}
		}
	}
	
	
	@Override
	public void onStart() {
	    super.onStart();
	    try {
	    	EasyTracker.getInstance(this).activityStart(this);  // Add this method.
		} catch (Exception e) {			
		}
	  }

	  @Override
	  public void onStop() {
	    super.onStop();
	    try {
	    	EasyTracker.getInstance(this).activityStop(this);  // Add this method.
		} catch (Exception e) {
		}
	  }	 
}
